package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.SpXiangmu;
import com.ruoyi.system.service.ISpXiangmuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SpCp;
import com.ruoyi.system.service.ISpCpService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 测评Controller
 * 
 * @author ruoyi
 * @date 2022-11-16
 */
@RestController
@RequestMapping("/system/cp")
public class SpCpController extends BaseController
{
    @Autowired
    private ISpCpService spCpService;

    @Autowired
    private ISpXiangmuService xiangmuService;


    /**
     * 查询测评列表
     */
    @PreAuthorize("@ss.hasPermi('system:cp:list')")
    @GetMapping("/list")
    public TableDataInfo list(SpCp spCp)
    {
        startPage();
        List<SpCp> list = spCpService.selectSpCpList(spCp);
        return getDataTable(list);
    }

    /**
     * 导出测评列表
     */
    @PreAuthorize("@ss.hasPermi('system:cp:export')")
    @Log(title = "测评", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SpCp spCp)
    {
        List<SpCp> list2 = new ArrayList<SpCp>();
        SpXiangmu xmquery = new SpXiangmu();
        xmquery.setScId(2l);
        List<SpXiangmu> xmlist =xiangmuService.selectSpXiangmuList(xmquery);
        List<SpCp> list = spCpService.selectSpCpList(spCp);
        for(SpCp cp:list){
            String name = "";
            if(cp.getXmids()!=null){
                String[] xmarr=  cp.getXmids().split(",");
                for(String xmid:xmarr){
                    SpXiangmu xm= new SpXiangmu();
                    for(SpXiangmu temp :xmlist){
                        if(temp.getXmId()==Long.parseLong(xmid)){
                            xm=temp;
                            break;
                        }
                    }
                    name+=xm.getName()+",";
                }
                cp.setXmnames(name);
            }

            list2.add(cp);
        }



        ExcelUtil<SpCp> util = new ExcelUtil<SpCp>(SpCp.class);
        util.exportExcel(response, list2, "测评数据");
    }

    /**
     * 获取测评详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:cp:query')")
    @GetMapping(value = "/{cpId}")
    public AjaxResult getInfo(@PathVariable("cpId") Long cpId)
    {
        return AjaxResult.success(spCpService.selectSpCpByCpId(cpId));
    }

    /**
     * 新增测评
     */
    @PreAuthorize("@ss.hasPermi('system:cp:add')")
    @Log(title = "测评", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SpCp spCp)
    {
        return toAjax(spCpService.insertSpCp(spCp));
    }

    /**
     * 修改测评
     */
    @PreAuthorize("@ss.hasPermi('system:cp:edit')")
    @Log(title = "测评", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SpCp spCp)
    {
        return toAjax(spCpService.updateSpCp(spCp));
    }

    /**
     * 删除测评
     */
    @PreAuthorize("@ss.hasPermi('system:cp:remove')")
    @Log(title = "测评", businessType = BusinessType.DELETE)
	@DeleteMapping("/{cpIds}")
    public AjaxResult remove(@PathVariable Long[] cpIds)
    {
        return toAjax(spCpService.deleteSpCpByCpIds(cpIds));
    }
}
